﻿    using Kinlo.Entitys;
using Kinlo.MESDocking;
using Microsoft.Extensions.DependencyInjection;
using NPOI.OpenXmlFormats.Spreadsheet;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using UIWindows.GlobalStatics;
using UIWindows.Models.Devices;
using UIWindows.Tools.Helper;
using XingLucifer.Devices;
using XingLucifer.IBase.Enums;

namespace UIWindows.Services.PLC_MESHelper
{
    /// <summary>
    /// 装盘完成
    /// </summary>
    public class PlateFinishedHandler : HelperBase
    {
        PLCBase InjMachine { get; set; }
        ReserveStatic reserveStatic { get; set; }
        MESWinderOutRecordAddRec _mesWinderOutRecord { get; set; }
        public PlateFinishedHandler(
                     DeviceInteractiveaddressStatic deviceInteractiveaddressStatic,
            DeviceClientModel deviceModel,
            TemporaryStatic temporaryStatic,
            ParameterStatic parameterStatic,
            DeviceConfigStatic deviceConfigStatic,
            SystemConfigStatic systemConfigStatic,
             HomeStatic homeStatic,
             Action<string, string, string, string, string, long, DateTime, string, string, string, string> log, Action<string, string, string> dialog, ReserveStatic _reserveStatic
            ) : base(deviceInteractiveaddressStatic, deviceModel, temporaryStatic, parameterStatic, deviceConfigStatic, systemConfigStatic, homeStatic, log, dialog)
        {
            InjMachine = deviceModel.PLC;
            reserveStatic = _reserveStatic;
            _mesWinderOutRecord = new MESWinderOutRecordAddRec(_log, _dialog);
        }
        public override Task Handle()
        {
            //KL21050011Machine InjMachine = null;
            short testResult = 2;
            try
            {
                //var dicts = ctx.Container.Get<ConcurrentDictionary<string, object>>();
                //var cfg = ctx.Container.Get<CFG>();
                //InjMachine = (KL21050011Machine)dicts.GetOrAdd("InjMachine", InjMachine);
                //var Counter = ctx.Container.Get<Counter>();
                //var data = ctx.Container.Get<PlateData>();
                if (true)
                {
                    reserveStatic.PlateScanBarcodeStatic.TrayBarcode = "";
                    reserveStatic.PlateScanBarcodeStatic.TrayBarcodeID = 0;
                    _parameter.TrayBarcode = "";
                    _parameter.TrayBarcodeID = 0;
                    testResult = 1;
                }
                else
                {
                    testResult = 2;
                }
            }
            catch (Exception ex)
            {
                RunLog( $"【装盘完成】发生异常:{ex}");
            }
            finally
            {
                var r1 = InjMachine.Write<short>("DM1209", new short[] { testResult });
                var r2 = InjMachine.Write<short>("DM1208", new short[] { 1 });
                var r3 = InjMachine.Write<short>("DM1008", new short[] { 0 });
                RunLog($"【装盘完成】写结果信号 {testResult}  {r1}.{r2}.{r3} {(r1 && r2 && r3 ? "成功" : "失败")}");
            }
            return base.Handle();
        }
    }
}
